import 'package:get/get.dart';
import 'package:safetymonitor/app/components/custom_modele/common_selection/common_selection_view.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/application/risk_point_entity.dart';
import 'package:safetymonitor/app/data/entities/application/security_control_entity.dart';
import 'package:safetymonitor/app/data/entities/auth/user_resp_entity.dart';
import 'package:safetymonitor/router/app_pages.dart';

class SnapShotController extends GetxController {
  /// 确认人
  UserRespEntity confirmBy = UserRespEntity();

  /// 风险点
  RiskPointEntity riskPoint = RiskPointEntity();

  /// 作业活动
  SecurityControlEntity securityControl = SecurityControlEntity();

  ///	隐患描述
  String description = '';

  /// 图片
  List<String> dangerImages = [];

  /// 视频
  List<String> dangerVideo = [];

  /// 选择隐患确认人
  void selectReportingPersonnel() {
    Get.toNamed(Routes.selectPeople)?.then((value) {
      if (value != null && value is UserRespEntity) {
        confirmBy = value;
        update();
      }
    });
  }

  /// 风险点选择
  void selectRiskPoint() {
    Get.to(()=>CommonSelectionPage<RiskPointEntity>(title: '风险点选择', enableLoad: false), arguments: {
      'interfaceUrl': Interface.getCurrentCompanyRisk,
    })?.then((value) {
      //如果不为空则设置信息
      if (value != null) {
        riskPoint = value[0] as RiskPointEntity;
        securityControl = SecurityControlEntity();
        update();
      }
    });
  }

  /// 作业活动/设备设施选择
  void selectWorkActivity() {
    if (riskPoint.id == 0 && riskPoint.riskName.isEmpty) {
      AppToast.showError('请选择风险点');
      return;
    }
    Get.to(()=>CommonSelectionPage<SecurityControlEntity>(title: '作业活动选择', enableLoad: false), arguments: {
      'interfaceUrl': Interface.getSecurityControl,
      'queryParameters': {'securityControlRiskPointId': riskPoint.id},
      'type': 'post',
    })?.then((value) {
      //如果不为空则设置信息
      if (value != null) {
        securityControl = value[0] as SecurityControlEntity;
        update();
      }
    });
  }

  /// 确定提交
  Future<void> confirm() async {
    if (confirmBy.id == 0 && confirmBy.getShowName().isEmpty) {
      AppToast.showError('请选择隐患确认人');
      return;
    }
    if (riskPoint.id == 0 && riskPoint.getShowName().isEmpty) {
      AppToast.showError('请选择风险点');
      return;
    }
    if (securityControl.id == 0 && securityControl.getShowName().isEmpty) {
      AppToast.showError('请选择作业活动');
      return;
    }
    if (dangerImages.isEmpty) {
      AppToast.showError('请上传隐患图片');
      return;
    }
    final String imgUrl = dangerImages.join(';');
    // 调用接口
    final res = await HttpUtil().generatePostRequest(Interface.postRiskImgCreate, data: {
      'confirmPersonId': confirmBy.id,
      'securityControlRiskPointId': riskPoint.id,
      'securityControlActivityId': securityControl.id,
      'description': description,
      'imgUrl': imgUrl,
      'videoUrl': dangerVideo.isNotEmpty ? dangerVideo[0] : ''
    }).execute(isShowLoading: true);

    res.onSuccess((result) {
      Get.back();
      AppToast.showSuccess('提交成功');
    });
  }

  /// 进入历史页面
  void goHistory() {
    Get.toNamed(Routes.snapShotHistory);
  }
}
